<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/5/14
 * Time: 13:03
 */
namespace app\admin\controller;

use app\admin\model\MemberComment;
use app\admin\validate\Member as validateMember;
use app\admin\model\Member as modelMember;
use think\db\Where;

class Member extends Base{
    protected $model = null;
    public function __construct(){
        parent::__construct();
        $this->model = new modelMember();
    }

    /**
     * @return \think\Response|\think\response\View
     * @throws \think\exception\DbException
     */
    public function index(){
        $where = new Where();
        $data = request()->post();
        if($data){
            if($data['nickname']){
                $where['nickname']=['like',"%".trim($data['nickname'])."%"];
            }
            if($data['open_id']){
                $where['open_id']=['like',"%".trim($data['open_id'])."%"];
            }
            if($data['date']){
                $t = explode('~',$data['date']);
                $start = strtotime($t[0]);
                $end = strtotime($t[1]);
                $where['create_time']= ['between time',[$start,$end]];
            }
        }
        $list = $this->model->getList($where,10);
        if(request()->isAjax()){
            return $this->__s('',$list);
        }else{
            $this->assign('total',$list->total());
            $this->assign('list',$list);
            return view();
        }
    }

    /**
     * 保存
     * @param int $id
     * @return array
     */
    public function save($id=0){
        sleep(1);
        $data = request()->post();
        $validate = new validateMember;
        if(!$validate->check($data)){
            return [
                'status'=>0,
                'msg'=>$validate->getError()
            ];
        }
        $column = new modelMember;
        if($id){
            if(!$column->allowField(true)->isUpdate(true)->save($data)){
                return [
                    'status'=>0,
                    'msg'=>'更新失败'
                ];
            }
            return [
                'status'=>1,
                'msg'=>'更新成功'
            ];
        }else{
            if(!$column->allowField(true)->save($data)){
                return [
                    'status'=>0,
                    'msg'=>'添加失败'
                ];
            }
            return [
                'status'=>1,
                'msg'=>'添加成功'
            ];
        }
    }

    /**
     * 删除
     * @param int $id
     * @param array $data
     * @return array
     */
    public function delete($id=0){
        sleep(1);
        if(!$id){
            return [
                'status'=>0,
                'msg'=>'缺少id'
            ];
        }
        $id = str_replace('_',',',$id);
        $column = new modelMember;
        if(!$column::destroy($id)){
            return [
                'status'=>0,
                'msg'=>'删除失败'
            ];
        }
        return [
            'status'=>1,
            'msg'=>'删除成功'
        ];
    }

    /**
     * 读取
     * @param int $id
     * @return array
     * @throws \think\exception\DbException
     */
    public function edit($id=0){
        if(!$id){
            return [
                'status'=>0,
                'msg'=>'缺少id'
            ];
        }
        $column = new modelMember;
        $_column = $column::get($id);
        if(!$_column){
            return [
                'status'=>0,
                'msg'=>'没有数据'
            ];
        }
        return [
            'status'=>1,
            'msg'=>'查询成功',
            'result'=>$_column
        ];
    }

    public function read($id=0){
        if(!$id){
            return [
                'status'=>0,
                'msg'=>'缺少id'
            ];
        }
        $column = new modelMember;
        $_column = $column::get($id);
        if(!$_column){
            return [
                'status'=>0,
                'msg'=>'没有数据'
            ];
        }
        return [
            'status'=>1,
            'msg'=>'查询成功',
            'result'=>$_column
        ];
    }

    public function update($id=0,$status=-1){
        if(!$id){
            return [
                'status'=>0,
                'msg'=>'缺少id'
            ];
        }
        $column = new modelMember;
        $_column = $column::get($id);
        if(!$_column){
            return [
                'status'=>0,
                'msg'=>'没有数据'
            ];
        }
        $_column->status=$status;
        if(!$_column->save()){
            return [
                'status'=>0,
                'msg'=>'修改失败'
            ];
        }
        return [
            'status'=>1,
            'msg'=>'修改成功'
        ];
    }

    /**
     * 禁用评论
     * @param int $limit
     * @return \think\Response|\think\response\View
     * @throws \think\exception\DbException
     */
    public function comment($limit=10){
        $where=new Where();
        $data = request()->post();
        $model = new MemberComment();
        $where[$model->getTable().'.mc_id'] = self::$mc_id;
        if($data){
            if($data['title']){
                $info = db('mch')->where('name','eq',$data['title'])->find();
                if($info){
                  $where[$model->getTable().'.mc_id']=['eq',$info['id']];
                }
            }
            if($data['status']){
                $where[$model->getTable().'.status']=$data['status'];
            }
            if($data['date']){
                $t = explode('~',$data['date']);
                $start = strtotime($t[0]);
                $end = strtotime($t[1]);
                $where[$model->getTable().'.create_time']= ['between time',[$start,$end]];
            }
        }
        $list = $model->getList($where,$limit);
        if(request()->isAjax()){
            return $this->__s('',$list);
        }else{
            $this->assign('total',$list->total());
            $this->assign('list',$list);
            return view();
        }
    }

    public function add_comment(){
        return view();
    }

    /**
     * @param int $id
     * @return bool|\think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function insert_comment($id=0){
        $data = request()->post();
        if(isset($data['title']) && $data['title']){
            $member = db('member')->where('nickname','eq',$data['title'])->find();
            if(!$member){
                return $this->__e('用户不存在');
            }
            $data['mid'] = $member['id'];
            unset($data['title']);
        }
        if(isset($data['mch']) && $data['mch']){
            $mch = db('mch')->where('name','eq',$data['mch'])->find();
            if(!$mch){
                return $this->__e('商户不存在');
            }
            $data['mc_id'] = $mch['id'];
            unset($data['mch']);
        }
        $model = new MemberComment();
        if($model->checkIt($data['mc_id'],$data['mid'])){
            $this->__e('已经添加了');
            return false;
        }
        if($id){
            if(!$model->isUpdate(true)->save($data)){
                $this->__e('操作失败');
            }
        }else{
            $data['create_time'] = time();
            if(!$model->insert($data)){
                $this->__e('操作失败');
            }
        }
        $this->__s('操作成功',['redirect'=>Url('member/comment')]);
    }

    /**
     * 删除评论人
     * @param int $id
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del_comment($id=0){
        if(!$id){
            $this->__e('缺少参数');
        }
        $m = new MemberComment();
        if(!$m->where('id','eq',$id)->delete()){
           $this->__e('删除失败');
        }
        $this->__s('删除成功');
    }

    /**
     *
     * @param int $id
     * @param int $status
     */
    public function comment_status($id=0,$status=0){
        if(!$id || !$status){
            $this->__e('缺少参数');
        }
        $m = new MemberComment();
        $info = $m->find($id);
        $info->status = $status;
        if(!$info->save()){
            $this->__e('操作失败');
        }
        $this->__s('操作成功');
    }
}